/**
 * Created by Maibenben on 2017/5/3.
 */
$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var m_id = 0;
    var mid = $.cookie("mid");
    var token = $.cookie("token");
    var important = "&mid=" + mid + "&token=" + token;
    var databack = false;
    var postchecked = false;
    var telphone;
    //倒计时
    var countdown = localStorage.getItem("countdowncon");
    var remainsecond = countdown;
    if(countdown){
        $(".codebtn").attr("disabled",true);
        var timer = setInterval(function () {
            remainsecond--;
            localStorage.setItem("countdowncon", remainsecond);
            if (remainsecond <= 0) {
                localStorage.removeItem("countdowncon");
                clearInterval(timer);
                $(".codebtn").val("重新获取").attr("disabled", false);
            } else {
                $(".codebtn").val(remainsecond + "s后可重新获取");
            }
        }, 1000);
    }
    //进入页面根据上次点击显示当前绑定信息
    $.ajax({
        url: bindURL + location.search + "&mid=" + mid + "&token=" + token,
        success: function (res) {
            databack = true;
            if (res.member.state == 1) {
                m_id = res.member.data.id;
                $(".uname").text(res.member.data.name);
                telphone = parseInt(res.member.data.mobile);
            }
            /*
             if (res.user.state == 1 && res.user.data.group_id == 3) {
             $(".bind-to").show();
             } else {
             $(".bind-to").remove();
             }
             */
        }
    });
    //点击获取验证码
    $(".codebtn").on("click", function () {
        postchecked = true;
        //倒计时
        localStorage.setItem("countdowncon", 60);
        var remainsecond = localStorage.getItem("countdowncon");
        $(this).attr("disabled", true);
        var timer = setInterval(function () {
            remainsecond--;
            localStorage.setItem("countdowncon", remainsecond);
            if (remainsecond <= 0) {
                localStorage.removeItem("countdowncon");
                clearInterval(timer);
                $(".codebtn").val("重新获取").attr("disabled", false);
            } else {
                $(".codebtn").val(remainsecond + "s后可重新获取");
            }
        }, 1000);
        //发送短信验证
        $.get(checkcodeURL + "?phone=" + telphone + "&type=5" + important, function (res) {
                creatLayout(res.msg);
            }
        );
    });
    /*
     //点击选取绑定主副卡
     $(".bind-to").on("click", function () {
     $(".choose_main").toggle();
     });
     $(".choose_main>div").on("click", function () {
     $(this).parent().hide();
     $(".option").text($(this).text()).attr("data-main", $(this).attr("data-main"));
     });
     */
    //点击绑定保存绑定卡号
    $(".footer").on("click", function () {
        var that = $(this);
        if (databack) {
            if ($(".option")) {
                var data = {
                    mid: mid,
                    token: token,
                    card_no: $(".ucard").val(),
                    m_id: m_id,
                    tap: $(".option").attr("data-main"),
                    type: 5,
                    captcha: $(".checkcode").val(),
                    phone:telphone
                };
            } else {
                var data = {
                    mid: mid,
                    token: token,
                    card_no: $(".ucard").val(),
                    m_id: m_id,
                    type: 5,
                    captcha: $(".checkcode").val(),
                    phone:telphone
                };
            }
            //将用户输入的数据发送到后台进行验证储存
            if (data.card_no == '') {
                creatLayout("请填写卡号");
                return false;
            }
            if ($(".checkcode").val() == "") {
                creatLayout("请输入验证码");
                return false;
            }
            databack = false;
            that.css("background", "#ccc");
            $.post(bindSaveURL, data, function (res) {
                databack = true;
                that.css("background", "rgb(47, 178, 159)");
                if (res['state'] == 1) {
                    window.location.href = "finish-reference.html";
                } else {
                    creatLayout(res['msg']);
                }
            });
        }
    });
});